<template>
  <a-modal
    title="派送订单"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="派送人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select v-model="sendUserId">
            <a-select-option :key="index" v-for="(item,index) in userList">
              {{ item.userName }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { toSend, sendUserDown } from '@/api/modular/main/system/bizOrderManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        statusData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        userList: [],
        selectList: [],
        sendUserId: null
      }
    },
    created() {
      this.getSendUserDown()
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        this.selectList = record
        console.log(record)
      },

      getSendUserDown () {
        sendUserDown().then((res) => {
          this.userList = res.data
        })
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            if (this.sendUserId == null) {
              this.$message.error('请选择派送用户')
            }
            if (this.selectList.length > 0) {
              const data = {
                ids: this.selectList,
                SendUserId: this.sendUserId
              }
              toSend(data).then((res) => {
                this.$message.success('派送成功')
              }).finally((res) => {
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              })
            } else {
              this.$message.error('派送失败')// + res.message
            }
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
